#ifndef BASE_CTMUTEX_H_
#define BASE_CTMUTEX_H_

#include <pthread.h>
#include <iostream>
#include "ct_noncopyable.h"

class ctMutex
{
	friend class ctCond;

	public:
	ctMutex(int kind = PTHREAD_MUTEX_FAST_NP)
	{
		pthread_mutexattr_t attr;
		::pthread_mutexattr_init(&attr);
		::pthread_mutexattr_settype(&attr, kind);
		::pthread_mutex_init(&mutex_, &attr);
	}
	~ctMutex()
	{
		::pthread_mutex_destroy(&mutex_);
	}
	inline void lock()
	{
		::pthread_mutex_lock(&mutex_);
	}
	inline void unlock()
	{
		::pthread_mutex_unlock(&mutex_);
	}

	private:
	pthread_mutex_t mutex_;
};

class ctMutex_scope_lock: private ctNoncopyable
{
	public:
		ctMutex_scope_lock(ctMutex &m)
			: mlock_(m)
		{
			mlock_.lock();
		}
		~ctMutex_scope_lock()
		{
			mlock_.unlock();
		}
			
	private:
		ctMutex &mlock_;
};
#endif //BASE_CTMUTEX_H_
